package com.github.yangyishe.p100;

import java.util.*;

public class Problem01_b {
    public static void main(String[] args) {
        int[] nums=new int[]{3,2,4};
        int target=6;

        Problem01_b problem01_b = new Problem01_b();
        int[] result = problem01_b.twoSum(nums, target);
        System.out.println("result = " + Arrays.toString(result));
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, List<Integer>> val2IndexListMap=new HashMap<>();
        for(int i=0;i<nums.length;i++){
            val2IndexListMap.putIfAbsent(nums[i],new ArrayList<>());
            val2IndexListMap.get(nums[i]).add(i);
        }

        for(int i=0;i<nums.length;i++){
            int req=target-nums[i];
            if(val2IndexListMap.get(req)!=null){
                for (Integer reqIndex : val2IndexListMap.get(req)) {
                    if (i!=reqIndex) {
                        return new int[]{i,reqIndex};
                    }
                }
            }
        }

        return null;
    }
}
